﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using System.Net;
using System.IO;
using Newtonsoft.Json.Linq;

namespace 天气预报
{
    class Program
    {
        // 获取天气数据，并返回字符串内容
        static string weatherString(string location, string time, string key)
        {
            string baseUrl = "https://free-api.heweather.net/s6/weather/";
            string fullUrl = baseUrl + time + "?location=" + location + "&key=" + key;
            try
            {
                WebClient client = new WebClient();
                client.Credentials = CredentialCache.DefaultCredentials;//获取或设置请求凭据
                Byte[] pageData = client.DownloadData(fullUrl); //下载数据
                string pageHtml = System.Text.Encoding.UTF8.GetString(pageData);
                return pageHtml;
            }
            catch (WebException ex)
            {
                return ex.Message;
            }

        }

        // 获取当前空气质量的数据
        static string airQltyString(string location, string key)
        {
            string baseUrl = "https://free-api.heweather.com/s6/air/now?location=" + location + "&key=" + key;
            try
            {
                WebClient client = new WebClient();
                client.Credentials = CredentialCache.DefaultCredentials;//获取或设置请求凭据
                Byte[] pageData = client.DownloadData(baseUrl); //下载数据
                string pageHtml = System.Text.Encoding.UTF8.GetString(pageData);
                return pageHtml;
            }
            catch (WebException ex)
            {
                return ex.Message;
            }
        }

        // 当前空气质量
        static string airQltyNow(string location, string key)
        {
            string jsonTextNow = airQltyString(location,key); // 获得空气质量数据
            JObject jo = (JObject)JsonConvert.DeserializeObject(jsonTextNow);
            string airQltyNow = jo["HeWeather6"][0]["air_now_city"]["qlty"].ToString();

            return "空气质量：" + airQltyNow;
        }

        // 解析当前天气数据
        static string weatherNow(string location, string key)
        {
            string time = "now";
            string jsonTextNow = weatherString(location, time, key);
            // 对json数据进行反序列化，读取所需数据
            JObject jo = (JObject)JsonConvert.DeserializeObject(jsonTextNow);
            string updateTime = jo["HeWeather6"][0]["update"]["loc"].ToString();
            string wlocation  = jo["HeWeather6"][0]["basic"]["location"].ToString();
            string nowCondTxt = jo["HeWeather6"][0]["now"]["cond_txt"].ToString();
            string nowTmp     = jo["HeWeather6"][0]["now"]["tmp"].ToString();
            string nowWindDir = jo["HeWeather6"][0]["now"]["wind_dir"].ToString();
            string nowWindSc  = jo["HeWeather6"][0]["now"]["wind_sc"].ToString();
                                          
            return "更新时间：" + updateTime + "\n"
                    + wlocation + "当前：" + nowCondTxt + "，" 
                    + nowTmp + "℃," + nowWindDir + nowWindSc + "级";
        }

        static string weatherForecast(string location, string key)
        {
            string time = "forecast";

            string jsonTextNow = weatherString(location, time, key);
            // 对json数据进行反序列化，读取所需数据
            JObject jo = (JObject)JsonConvert.DeserializeObject(jsonTextNow);
            // 今天的预报数据
            string todDate     = jo["HeWeather6"][0]["daily_forecast"][0]["date"].ToString();
            string todCondTxtD = jo["HeWeather6"][0]["daily_forecast"][0]["cond_txt_d"].ToString();
            string todCondTxtN = jo["HeWeather6"][0]["daily_forecast"][0]["cond_txt_n"].ToString();
            string todWindDir  = jo["HeWeather6"][0]["daily_forecast"][0]["wind_dir"].ToString();
            string todWindSc   = jo["HeWeather6"][0]["daily_forecast"][0]["wind_sc"].ToString();
            string todTmpMin   = jo["HeWeather6"][0]["daily_forecast"][0]["tmp_min"].ToString();
            string todTmpMax   = jo["HeWeather6"][0]["daily_forecast"][0]["tmp_max"].ToString();
            
            // 明天的预报数据
            string tomDate      = jo["HeWeather6"][0]["daily_forecast"][1]["date"].ToString();
            string tomCondTxtD  = jo["HeWeather6"][0]["daily_forecast"][1]["cond_txt_d"].ToString();
            string tomCondTxtN  = jo["HeWeather6"][0]["daily_forecast"][1]["cond_txt_n"].ToString();
            string tomWindDir   = jo["HeWeather6"][0]["daily_forecast"][1]["wind_dir"].ToString();
            string tomWindSc    = jo["HeWeather6"][0]["daily_forecast"][1]["wind_sc"].ToString();
            string tomTmpMin    = jo["HeWeather6"][0]["daily_forecast"][1]["tmp_min"].ToString();
            string tomTmpMax    = jo["HeWeather6"][0]["daily_forecast"][1]["tmp_max"].ToString();

            //后天的预报数据
            string aftTomDate     = jo["HeWeather6"][0]["daily_forecast"][2]["date"].ToString();
            string aftTomCondTxtD = jo["HeWeather6"][0]["daily_forecast"][2]["cond_txt_d"].ToString();
            string aftTomCondTxtN = jo["HeWeather6"][0]["daily_forecast"][2]["cond_txt_n"].ToString();
            string aftTomWindDir  = jo["HeWeather6"][0]["daily_forecast"][2]["wind_dir"].ToString();
            string aftTomWindSc   = jo["HeWeather6"][0]["daily_forecast"][2]["wind_sc"].ToString();
            string aftTomTmpMin   = jo["HeWeather6"][0]["daily_forecast"][2]["tmp_min"].ToString();
            string aftTomTmpMax   = jo["HeWeather6"][0]["daily_forecast"][2]["tmp_max"].ToString();


            return "今天(" + todDate + ")，" + todCondTxtD + "-" + todCondTxtN + "，" + todWindDir + todWindSc + "级，" + todTmpMin + "-" + todTmpMax +"℃。\n"
                  + "明天(" + tomDate + ")，" + tomCondTxtD + "-" + tomCondTxtN + "，" + tomWindDir + tomWindSc + "级，" + tomTmpMin + "-" + tomTmpMax + "℃。\n"
                  + "后天(" + aftTomDate + ")，" + aftTomCondTxtD + "-" + aftTomCondTxtN + "，" + aftTomWindDir + aftTomWindSc + "级，" + aftTomTmpMin + "-" + aftTomTmpMax + "℃。"
                  ;        // 这里有个分号，别忘了，上面三行没有分号，return语句在这里结尾
        }

        static void Main(string[] args)
        {
            string key = "322389fe745246a88c9371a867475438";
            string location = "auto_ip";

            string fullWeather = weatherNow(location,key );
            string airQlty = airQltyNow(location,key);
            string weather3Day = weatherForecast(location,key);

            Console.WriteLine("------------------------------------------------------");
            Console.WriteLine(fullWeather);
            Console.WriteLine(airQlty);
            Console.WriteLine("------------------------------------------------------");
            Console.WriteLine(weather3Day);
            Console.WriteLine("------------------------------------------------------");
            Console.ReadLine();
        }
    }

}